package com.ruibang.glass.quality.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.domain.file.FileBusiness;
import com.ruibang.glass.quality.domain.req.OriginalBoardReq;
import com.ruibang.glass.quality.entity.OriginalBoard;
import com.ruibang.glass.quality.feign.FileApi;
import com.ruibang.glass.quality.mapper.OriginalBoardMapper;
import com.ruibang.glass.quality.service.OriginalBoardService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 来料-原板 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-05-21
 */
@Slf4j
@Service
public class OriginalBoardServiceImpl extends ServiceImpl<OriginalBoardMapper, OriginalBoard> implements OriginalBoardService {

    @Autowired
    private FileApi fileApi;


    @Override
    public IPage<OriginalBoard> pageQuery(OriginalBoardReq originalBoardReq) {
        return null;
    }

    @Override
    public void saveInfo(OriginalBoard originalBoard) {
        //保存
        this.save(originalBoard);
        //保存文件
        if (CollectionUtils.isNotEmpty(originalBoard.getFileIds())) {
            FileBusiness fileBusiness = new FileBusiness();
            fileBusiness.setFileIds(originalBoard.getFileIds());
            fileBusiness.setBusinessId(originalBoard.getId());
            fileApi.updateBusinessById(fileBusiness);
        }
    }

    @Override
    public void edit(OriginalBoard originalBoard) {
        //修改
        this.updateById(originalBoard);
        //修改文件
        if (CollectionUtils.isNotEmpty(originalBoard.getFileIds())) {
            FileBusiness fileBusiness = new FileBusiness();
            fileBusiness.setFileIds(originalBoard.getFileIds());
            fileBusiness.setBusinessId(originalBoard.getId());
            fileApi.updateBusinessById(fileBusiness);
        }
    }

    @Override
    public OriginalBoard importExcel(MultipartFile file) {
        if (file != null) {
            //上传文件
            ResultBody<String> resultBody = fileApi.uploadFile(file);
            OriginalBoard originalBoard = new OriginalBoard();
            if (resultBody != null && StringUtils.isNotBlank(resultBody.getData())) {
                originalBoard.setUpLoadFileId(resultBody.getData());
            }

            // 创建一个 XSSFWorkbook 对象
            Workbook workbook = null;
            try {
                workbook = new XSSFWorkbook(file.getInputStream());
            } catch (Exception e) {
                //如果不是excel直接上传文件返回
                return originalBoard;
            }

            // 获取第一个工作表
            Sheet sheet = workbook.getSheetAt(0);


            //
            return originalBoard;
        }

        return null;
    }


}
